package com.bitcat.todoapp.demo.dbdemo

import android.os.Bundle
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.bitcat.todoapp.R
import com.bitcat.todoapp.app.db.DataBaseHelper
import es.dmoral.toasty.Toasty

class SqliteDemoActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_sqlite_demo)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
    }

    fun insertUser(view: View) {
        val helper = DataBaseHelper(this)
        val result = helper.registerUser("test3", "123456")
        if (result) {
            Toasty.success(this, "注册成功", Toasty.LENGTH_SHORT).show()
        } else {
            Toasty.error(this, "注册失败", Toasty.LENGTH_SHORT).show()
        }
    }


    fun queryUser(view: View) {
        val helper = DataBaseHelper(this)
        if (helper.checkUserExist("test")) {
            Toasty.success(this, "查询成功", Toasty.LENGTH_SHORT).show()
        } else {
            Toasty.error(this, "查询失败", Toasty.LENGTH_SHORT).show()
        }
    }

    fun deleteUser(view: View) {
        val helper = DataBaseHelper(this)
        val result = helper.deleteUser("test2")
        Toasty.success(this, result, Toasty.LENGTH_SHORT).show()
    }

    fun updateUser(view: View) {
        val helper = DataBaseHelper(this)
        val result = helper.updateUser("test2", "000000")
        Toasty.success(this, result, Toasty.LENGTH_SHORT).show()
    }

    fun queryAllUser(view: View) {
        val helper = DataBaseHelper(this)
        val users = helper.queryAllUsers()
        println(users)
    }

    fun login(view: View) {
        val db = DataBaseHelper(this)
        val isSuccessFul = db.login("test2", "123456")
        if (isSuccessFul) {
            Toasty.success(this, "登录成功", Toasty.LENGTH_SHORT).show()
        } else {
            Toasty.error(this, "登录失败", Toasty.LENGTH_SHORT).show()
        }
    }
}

